package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.persistence.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SignInService {
    @Autowired
    private AccountMapper AccountMapper;
    public SignInService(){

    }
    public Account getAccount(String username) {
        return AccountMapper.getAccountByUsername(username);
    }

    public Account getAccount(String username, String password) {
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return AccountMapper.getAccountByUsernameAndPassword(account);
    }

    public void insertAccount(Account account) {
        AccountMapper.insertAccount(account);
        AccountMapper.insertProfile(account);
        AccountMapper.insertSignon(account);
    }

    public void updateAccount(Account account) {
        AccountMapper.updateAccount(account);
        AccountMapper.updateProfile(account);

        if (account.getPassword() != null && account.getPassword().length() > 0) {
            AccountMapper.updateSignon(account);
        }
    }

    public List<Account> getallAccount(){
        return AccountMapper.getallAccount();
    }

    public List<Account> getallAccountprofile(){
        return AccountMapper.getallAccountprofile();
    }

    public List<Account> searchAccount(String keyword){
        return AccountMapper.searchAccount("%" + keyword.toLowerCase() + "%");
    }

    public List<Account> searchAccountprofile(String keyword){
        return AccountMapper.searchAccountprofile(keyword);
    }

    public void deleteAccount(String username){
        AccountMapper.deleteAccount(username);
        AccountMapper.deleteprofile(username);
        AccountMapper.deleteSignon(username);
    }

}
